Kanzi has the following memory managers:
The application framework first reserves the amount of memory you define from the system using the system memory manager. Then the memory is split into memory pools by the pooled memory manager. The quick memory manager is used for very fast temporary data storage. It tracks only the available memory with one pointer pointing to the available space.
The default memory manager you access when reserving memory for your application is the pooled memory manager. The pooled memory manager is optimized to minimize memory fragmentation and ensures good performance during the entire application lifetime.
The system memory manager uses ordinary malloc and free functions for reserving and freeing memory, which is slower in most cases but is useful for debugging memory, for example, using external memory debugging tools. Otherwise, you do not have to set the memory pool count in configuration. Memory pools are generated as needed during the application runtime.
With the quick memory manager all allocations are made linearly, and deallocation of single blocks is not supported at all. You can reset the quick memory at once, freeing the entire memory. The quick memory manager provides fast memory allocation for temporary, real-time use. It is used by the Kanzi Engine for making lots of small temporary allocations quickly in the update and rendering loop. All the allocated memory is then discarded at the end of the loop. Another typical use of the quick memory manager is for text parsing tasks: you allocate a large enough chunk of memory for the quick memory manager, then access the memory many times consecutively during the task, and finally discard all data after the task is done.
The amount of memory needed by an application is often slightly larger than the size of the Kanzi Studio binary loaded, but depending on the application, can also be higher. If the amount of memory you reserve is not large enough, the application does not start and produces an out-of-memory-error instead. It is a good practice to find a value that is slightly larger than needed for not impairing performance while preserving the system memory. Note that increasing the reserved memory can result in a higher frame rate.
When implementing the application framework, you have to pass the amount of memory that your application reserves in the system memory. The total allocated memory for the pooled memory manager in bytes is memoryPoolCount * memoryPoolSize.
For example, the following command in kzApplicationConfigure tells the system memory manager to reserve ten megabytes.
configuration->memoryPoolSize = 10 * 1024 * 1024;
You can access the pooled memory manager by using
memoryManager = kzaApplicationGetApplicationMemoryManager(application);
You can disable the pooled memory manager by setting the memory pool count to 0. In this case your Kanzi application uses the system memory manager.
configuration->memoryPoolCount = 0;
To create a quick memory manager use
// Create a quick memory manager and reserve 1 MB kzcMemoryManagerCreateQuickManager(memoryManager, 1 * 1024 * 1204, &myQuickMemoryManager)
You can use already created quick memory manager during application development. For example, you can retrieve it from the renderer by calling
kzuRendererGetQuickMemoryManager
It is important to reset the quick memory manager from time to time (for example, per frame).
To reset the quick memory manager use
kzcMemoryManagerResetQuickManager(quickMemoryManager)
You can retrieve the memory manager responsible for Kanzi objects with kzcMemoryGetManager by passing a pointer to the object.
memoryManager = kzcMemoryGetManager(objectPointer);
Running Kanzi applications in multithread environments
Memory management best practices